package cn.schoolwow.workflow.test.module.gateway.flow;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.dto.FlowContextDataRange;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.workflow.QuickWorkFlow;
import cn.schoolwow.workflow.domain.instance.StartWorkFlowRequest;
import cn.schoolwow.workflow.domain.instance.WorkFlowInstanceStatus;
import cn.schoolwow.workflow.domain.task.CompleteTaskRequest;
import cn.schoolwow.workflow.domain.task.TaskType;
import cn.schoolwow.workflow.domain.task.WorkFlowTaskQuery;
import cn.schoolwow.workflow.domain.task.WorkFlowTaskResponse;
import cn.schoolwow.workflow.entity.WorkFlowInstance;
import org.apache.commons.lang3.Validate;

import java.util.Arrays;

public class StarLeaveConditionAuditFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        startWorkFlow(flowContext);
        submitLeaveDay(flowContext);
        auditLeaveTask(flowContext);
    }

    @Override
    public String name() {
        return "启动请假排他网关工作流审批";
    }

    private void startWorkFlow(FlowContext flowContext){
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);
        String initiator = flowContext.checkData("initiator", String.class);

        StartWorkFlowRequest startWorkFlowRequest = new StartWorkFlowRequest();
        startWorkFlowRequest.definitionName = "请假流程排他网关审批";
        startWorkFlowRequest.initiator = initiator;
        flowContext.putInstanceData(startWorkFlowRequest, FlowContextDataRange.ContextThreadLocal);

        WorkFlowInstance workFlowInstance = quickWorkFlow.startWorkFlow(startWorkFlowRequest);
        flowContext.putInstanceData(workFlowInstance);
    }

    private void submitLeaveDay(FlowContext flowContext){
        WorkFlowInstance workFlowInstance = flowContext.checkInstanceData(WorkFlowInstance.class);
        StartWorkFlowRequest startWorkFlowRequest = flowContext.checkInstanceData(StartWorkFlowRequest.class);
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);
        int leaveDay = flowContext.checkData("leaveDay", int.class);

        WorkFlowTaskQuery workFlowTaskQuery = new WorkFlowTaskQuery();
        workFlowTaskQuery.instanceIdList = Arrays.asList(workFlowInstance.getId());
        workFlowTaskQuery.initiatorList = Arrays.asList(startWorkFlowRequest.initiator);
        workFlowTaskQuery.instanceStatusList = Arrays.asList(WorkFlowInstanceStatus.Running.status);

        WorkFlowTaskResponse workFlowTaskResponse = quickWorkFlow.getSingleWorkFlowTask(workFlowTaskQuery);
        Validate.notNull(workFlowTaskResponse, "设置请假天数任务获取为空");
        CompleteTaskRequest completeTaskRequest = new CompleteTaskRequest();
        completeTaskRequest.taskId = workFlowTaskResponse.taskId;
        completeTaskRequest.taskType = TaskType.COMPLETE;
        completeTaskRequest.contextData.put("请假天数", leaveDay);
        quickWorkFlow.completeTask(completeTaskRequest);
    }

    private void auditLeaveTask(FlowContext flowContext){
        WorkFlowInstance workFlowInstance = flowContext.checkInstanceData(WorkFlowInstance.class);
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);
        String auditAssigner = flowContext.checkData("auditAssigner", String.class);

        WorkFlowTaskQuery workFlowTaskQuery = new WorkFlowTaskQuery();
        workFlowTaskQuery.assignerList = Arrays.asList(auditAssigner);
        workFlowTaskQuery.instanceIdList = Arrays.asList(workFlowInstance.getId());
        workFlowTaskQuery.instanceStatusList = Arrays.asList(WorkFlowInstanceStatus.Running.status);
        WorkFlowTaskResponse workFlowTaskResponse = quickWorkFlow.getSingleWorkFlowTask(workFlowTaskQuery);
        Validate.notNull(workFlowTaskResponse, "请假审批人查询为空!审批人:"+auditAssigner);
        quickWorkFlow.completeTask(workFlowTaskResponse.taskId, TaskType.COMPLETE);
    }

}
